package com.aerozhonghuan.logisticsmall.modules.home.search;

import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.DB.DBAdapter;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarActivity;
import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.aerozhonghuan.logisticsmall.modules.common.entity.SearchProductHistory;
import com.aerozhonghuan.logisticsmall.modules.home.logic.HomePageInfoRequest;
import com.aerozhonghuan.logisticsmall.modules.home.search.beans.SearchBean;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.logisticsmall.widget.TitleBarView;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;
import com.blankj.utilcode.util.KeyboardUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * 描述 搜索页面
 *
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class SearchAcrivity extends TitlebarActivity {

    private TitleBarView titleBarView;
    private ProgressDialogIndicator dialogIndicator;
    private String mKeyWord;
    private Set<String> mKeys = new HashSet<String>();
    public DBAdapter mDbAdapter;
    public ArrayList<SearchProductHistory> mSearchProductHistories;
    private EditText editView;
    private View.OnClickListener onSearchClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            KeyboardUtils.hideSoftInput(getActivity());
            mKeyWord = titleBarView.getEditText();
            if (TextUtils.isEmpty(mKeyWord)) {
                alert(getString(R.string.search_product));
                return;
            }
            if (mKeys.add(mKeyWord)) {
                mDbAdapter.open().insertSearchProductHistory(System.currentTimeMillis(), mKeyWord);
            } else {
                boolean d = mDbAdapter.open().deleteSearchProductByKeyData(mKeyWord);
                if (d) {
                    mDbAdapter.open().insertSearchProductHistory(System.currentTimeMillis(), mKeyWord);
                }
            }
            searchProduct(mKeyWord);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            mDbAdapter = new DBAdapter(this);
            initView();
            initData();
            showFragment(new HistoryKeyFragment(), false);
        }
    }

    public void initData() {
        mSearchProductHistories = mDbAdapter.querySearchProductHistory();
        if (mSearchProductHistories.size() > 0) {
            for (SearchProductHistory productHistory : mSearchProductHistories) {
                mKeys.add(productHistory.getProcutName());
            }
        }
    }

    private void initView() {
        dialogIndicator = new ProgressDialogIndicator(this);
        titleBarView = getTitleBarView();
        titleBarView.setTitleVisibility(false);
        titleBarView.showRightText("搜索", onSearchClick);
        editView = titleBarView.getEditView();
        editView.setVisibility(View.VISIBLE);
        editView.setHint("请输入商品名称");
        editView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String string = editable.toString();
                if (TextUtils.isEmpty(string)) {
                    showFragment(new HistoryKeyFragment(), false);
                } else {
                    Selection.setSelection(editable, string.length());
                }
            }
        });
    }

    public void searchProduct(final String keyWord) {
        //请求数据
        HomePageInfoRequest.search(keyWord, "100", "1", this, dialogIndicator, new CommonCallback<SearchBean>() {
            @Override
            public void onSuccess(SearchBean messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                ArrayList<GoodsObject> products = messsageBodyObject.datalist;
                Bundle bundle = new Bundle();
                bundle.putSerializable("search_res", products);
                showFragment(new SearchProductFragment(), false, false, bundle);
            }
        });
    }
}
